package main

import (
	"encoding/json"
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
	_ "github.com/mattn/go-sqlite3"
)

func postUserHandler(c *gin.Context) {
	var u User
	u.Username = c.PostForm("username")
	u.Addr = c.PostForm("addr")
	u.Password = c.PostForm("password")
	u.Phone = c.PostForm("phone")
	fmt.Println(u)
	err := insertUser(u.Username, u.Password, u.Addr, u.Phone)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"ok":   false,
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"ok":   true,
		"data": nil,
	})
}

func getUserHandler(c *gin.Context) {
	username := c.Query("username")
	password := c.Query("password")
	u, err := getUser(username)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"ok":   false,
			"data": "用户不存在",
		})
		return
	}
	if u.Password == password {
		u.Password = ""
		c.JSON(http.StatusOK, gin.H{
			"ok":   true,
			"data": u,
		})
		return
	} else {
		c.JSON(http.StatusOK, gin.H{
			"ok":   false,
			"data": "密码错误",
		})
		return
	}
}

func postGoodsHandler(c *gin.Context) {
	var g Goods
	g.Describe = c.PostForm("describe")
	g.GoodsType = c.PostForm("type")
	g.Name = c.PostForm("name")
	g.Ower = c.PostForm("ower")
	if g.Describe == "" || g.GoodsType == "" || g.Name == "" || g.Ower == "" {
		c.JSON(http.StatusOK, gin.H{
			"ok":   false,
			"data": "缺少数据",
		})
		return
	}
	g.Status = "待出售"
	err := insertGoods(g.Ower, g.Status, g.GoodsType, g.Describe, g.Name)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"ok":   false,
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"ok":   true,
		"data": nil,
	})
}

func searchGoodsHandler(c *gin.Context) {
	keywords := c.Query("keywords")
	goodsList, err := searchGoods(keywords)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"ok":   false,
			"data": err.Error(),
		})
		return
	}
	data, _ := json.Marshal(goodsList)
	c.JSON(http.StatusOK, gin.H{
		"ok":   true,
		"data": string(data),
	})
}

func getGoodsHandler(c *gin.Context) {
	name := c.Query("name")
	ower := c.Query("ower")
	goodsList, err := getGoods(name, ower)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"ok":   false,
			"data": err.Error(),
		})
		return
	}
	data, _ := json.Marshal(goodsList)
	c.JSON(http.StatusOK, gin.H{
		"ok":   true,
		"data": string(data),
	})
}

func putUserHandler(c *gin.Context) {
	var u User
	u.Password = c.PostForm("password")
	u.Username = c.PostForm("username")
	u.Addr = c.PostForm("addr")
	u.Phone = c.PostForm("phone")

	err := updateUser(u.Username, u.Password, u.Addr, u.Phone)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"ok":   false,
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"ok":   true,
		"data": "",
	})
}

func getUserinfo(c *gin.Context) {
	username := c.Query("username")
	u, err := getUser(username)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"ok":   false,
			"data": err.Error(),
		})
		return
	}
	data, _ := json.Marshal(u)
	c.JSON(http.StatusOK, gin.H{
		"ok":   true,
		"data": string(data),
	})
}

func main() {
	initDB()
	r := gin.Default()
	r.POST("/user", postUserHandler)
	r.GET("/user", getUserHandler)
	r.PUT("/user", putUserHandler)

	r.POST("/goods", postGoodsHandler)
	r.GET("/goods", getGoodsHandler)
	// r.PUT("/goods", )

	r.GET("/userinfo", getUserinfo)

	r.GET("/searchgoods", searchGoodsHandler)
	r.Run(":8888")
}
